﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Aribaccio.Custom
{
    /// <summary>
    ///  枚举字符串转换器
    /// </summary>
    public class EnumConverter : IValueConverter
    {
        /// <summary>
        /// 枚举转字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Enum enumValue)
            {
                string strResult = enumValue.ToDescription();
                if (!string.IsNullOrWhiteSpace(strResult)) return strResult;
                strResult = enumValue.ToDisplayName();
                if (!string.IsNullOrWhiteSpace(strResult)) return strResult;
                strResult = enumValue.ToStringName();
                if (strResult.Equals("none", StringComparison.OrdinalIgnoreCase))
                {
                    return string.Empty;
                }
            }
            return value;
        }
        /// <summary>
        /// 字符串转枚举
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
